package com.loansystem.controller.user;

import com.loansystem.bean.user.Loans;
import com.loansystem.bean.user.User;
import com.loansystem.service.user.UserService;
import com.loansystem.utils.FileUpload;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

@Controller
public class UserController {

    @Autowired
    UserService userService;

    //跳转修改页
    @RequestMapping("/user/goUpdateUserInfo")
    public String goUpdateUserInfo() {
        return "user/updateUserInfo";
    }
    //跳转贷款申请页
    @RequestMapping("/user/goLoanApplication")
    public String goLoanApplication() {
        return "user/loanApplication";
    }
    //跳转查看申请状态页
    @RequestMapping("/user/goLoansInfo")
    public String goLoansInfo() {
        return "user/getLoansInfo";
    }


    //显示用户信息
    @RequestMapping("/user/userInfo")
    public String goUserInfo(Model model) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        model.addAttribute("user", user);
        return "user/userInfo";
    }

    //获取用户信息，用于修改
    @RequestMapping("/user/getUserInfo")
    @ResponseBody
    public Map<String, User> getUserInfo() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Map map = new HashMap();
        map.put("user", user);
        return map;
    }

    //修改用户信息
    @RequestMapping("/user/updateUserInfo")
    @ResponseBody
    public boolean updateUserInfo(User user, MultipartFile userImg) {
        System.out.println(userImg);
        if (userImg != null) {
            //上传头像
            FileUpload.doSingleFileUpload(userImg, "images/user", "userImg", user, "user_img");
        }
        System.out.println(user.getUser_sex());
        int i = userService.updataUserInfo(user);
        if (i > 0) {
            return true;
        }
        return false;
    }

    //提交贷款信息
    @RequestMapping("/user/submitLoansInfo")
    @ResponseBody
    public String submitLoansInfo(Loans loans, MultipartFile idCardImg, MultipartFile billImg, MultipartFile certificateOfEmploymentImg, MultipartFile creditReportImg, MultipartFile housingOwnershipCertificateImg) {
        User user = userService.getUserById(loans.getUser_id());
        if(user.getRand_card().equals("暂无")){
            return "2";
        }
        if (idCardImg != null) {
            FileUpload.doSingleFileUpload(idCardImg, "images/loans", "loansImg", loans, "id_card_img");
        }
        if (billImg != null) {
            FileUpload.doSingleFileUpload(billImg, "images/loans", "loansImg", loans, "bill_img");
        }
        if (certificateOfEmploymentImg != null) {
            FileUpload.doSingleFileUpload(certificateOfEmploymentImg, "images/loans", "loansImg", loans, "certificate_of_employment_img");
        }
        if (creditReportImg != null) {
            FileUpload.doSingleFileUpload(creditReportImg, "images/loans", "loansImg", loans, "credit_report_img");
        }
        if (housingOwnershipCertificateImg != null) {
            FileUpload.doSingleFileUpload(housingOwnershipCertificateImg, "images/loans", "loansImg", loans, "housing_ownership_certificate_img");
        }
        loans.setLoans_date(LocalDateTime.now());
        loans.setDue_date(LocalDateTime.now().plusYears(3));
        int i = userService.submitLoansInfo(loans);
        if (i > 0) {
            return "0";
        }
        return "1";
    }

    //获取贷款信息
    @RequestMapping("/user/getLoansInfo")
    @ResponseBody
    public List<Map<String,Object>> getLoansInfo(Model model) {
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        model.addAttribute("user",user);
        //获取用户的所有贷款提交信息
        List<Loans> loansInfo = userService.getLoansInfo(user.getUser_id());
        List<Map<String,Object>> resultList = new ArrayList<>();

        for (int i=0;i<loansInfo.size();i++){
            Map<String,Object> infoMap = new HashMap<>();
            Map<String,Object> imagesMap = new HashMap<>();
            infoMap.put("user_mail",user.getUser_mail());
            infoMap.put("Loans_money",loansInfo.get(i).getLoans_money());
            infoMap.put("Loans_purpose",loansInfo.get(i).getLoans_purpose());
            infoMap.put("Loans_date",loansInfo.get(i).getLoans_date());
            infoMap.put("Due_date",loansInfo.get(i).getDue_date());
            //设置状态, 设置前端tag标签的类型
            if(loansInfo.get(i).getLoans_state()==1){
                infoMap.put("Loans_state","申请已提交");
                infoMap.put("stateType","");
            }else if(loansInfo.get(i).getLoans_state()==2){
                infoMap.put("Loans_state","正在处理");
                infoMap.put("stateType","warning");
            }else if(loansInfo.get(i).getLoans_state()==3){
                infoMap.put("Loans_state","已完成");
                infoMap.put("stateType","success");
            }else if(loansInfo.get(i).getLoans_state()==4){
                infoMap.put("Loans_state","请求被驳回");
                infoMap.put("stateType","danger");
            }
            //设置文件
            if(loansInfo.get(i).getId_card_img()!=null){
                imagesMap.put("身份证正反面",loansInfo.get(i).getId_card_img());
            }
            if(loansInfo.get(i).getBill_img()!=null){
                imagesMap.put("近六个月的流水",loansInfo.get(i).getBill_img());
            }
            if(loansInfo.get(i).getCertificate_of_employment_img()!=null){
                imagesMap.put("工作证明",loansInfo.get(i).getCertificate_of_employment_img());
            }
            if(loansInfo.get(i).getCredit_report_img()!=null){
                imagesMap.put("征信报告",loansInfo.get(i).getCredit_report_img());
            }
            if(loansInfo.get(i).getHousing_ownership_certificate_img()!=null){
                imagesMap.put("房屋产权证",loansInfo.get(i).getHousing_ownership_certificate_img());
            }
            infoMap.put("fileImg",imagesMap);
            resultList.add(infoMap);
        }
        return resultList;
    }

}
